﻿using BCCommon;
using BCData.CRM.CustomerFollowUpRecord;
using BCData.CRM.CustomerInfo;
using BCData.Sim.Admin.Admin;
using BCDto.CRM.CustomerFollowUpRecord;
using BCDto.Sim.Admin.Admin;
using BCEntity.CRM.CustomerFollowUpRecord;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace BCService.CRM.CustomerFollowUpRecord
{
    public class CustomerFollowUpRecordService : ICustomerFollowUpRecordService
    {
        private ICustomerFollowUpRecordData customerFollowUpRecordData;
        private ICustomerInfoData customerInfoData;

        public CustomerFollowUpRecordService(ICustomerFollowUpRecordData ccCustomerFollowUpRecordData, ICustomerInfoData ccCustomerInfoData)
        {
            customerFollowUpRecordData = ccCustomerFollowUpRecordData;
            customerInfoData = ccCustomerInfoData;
        }

        #region Insert
        public CustomerFollowUpRecordDto Insert(AdminDto admin, CustomerFollowUpRecordRequestDto ccDto)
        {
            var entity = ccDto.As<CustomerFollowUpRecordEntity>();
            entity.CompanyId = admin.CompanyId;

            entity.CreateTime = DateTime.Now;
            entity.AdminId = admin.AdminId;
            entity.AdminFullName = admin.FullName;
            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminFullName = admin.FullName;
            entity.ScheduleTime = DateTime.Now;
            entity.TimeoutDate = DateTime.Now.AddMonths(3);
            entity.IsTimeout = false;
            entity.IsGivenUp = false;
            entity.IsShake = false;
            CheckInput(entity); 
            return customerFollowUpRecordData.InsertAsync(entity).GetAwaiter().GetResult().As< CustomerFollowUpRecordDto>();  
        }
        #endregion

        #region CheckInput
        private void CheckInput(CustomerFollowUpRecordEntity ccDo)
        {
            var fCustomer = customerInfoData.GetByIdAsync(ccDo.CustomerInfoId).GetAwaiter().GetResult() ?? throw new ArgumentException("查不到客户信息!");
            ccDo.UserName = fCustomer.UserName;
        }
        #endregion

        #region update
        public CustomerFollowUpRecordDto Update(AdminDto admin, CustomerFollowUpRecordDto dto)
        {
            var entity = dto.As<CustomerFollowUpRecordEntity>();
            if (entity.CompanyId <= 0)
                entity.CompanyId = admin.CompanyId;

            entity.AdminId = admin.AdminId;
            entity.AdminFullName = admin.FullName;
            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminFullName = admin.FullName;
            //fEntity.ScheduleTime = DateTime.Now;
            entity.TimeoutDate = DateTime.Now.AddMonths(3);
            entity.IsTimeout = false;
            entity.IsGivenUp = false;
            entity.IsShake = false;
            CheckInput(entity); 
            return customerFollowUpRecordData.UpdateAsync(entity).GetAwaiter().GetResult().As< CustomerFollowUpRecordDto>();
 
        }
        #endregion

        #region Delete
        public void Delete(long id)
        {
           customerFollowUpRecordData.Delete(id).GetAwaiter().GetResult(); 
        }
        #endregion

        #region GetByID
        public CustomerFollowUpRecordDto GetByID(long id)
        {
            var a = customerFollowUpRecordData.GetById(id).GetAwaiter().GetResult() ; 
                return a.As<CustomerFollowUpRecordDto>(); 
        }
        #endregion

        #region GetList
        public PageableList<CustomerFollowUpRecordDto> GetList(AdminDto admin, int pageNo,int pageSize)
        {
            var (fCount,fList) = customerFollowUpRecordData.GetListAsync(admin.CompanyId,pageNo, pageSize).GetAwaiter().GetResult() ;
            return new PageableList<CustomerFollowUpRecordDto>()
            {
                Count = fCount,
                Items = fList.As<List<CustomerFollowUpRecordDto>>(),
                PageIndex = pageNo,
                PageSize = pageSize
            };
        }
        #endregion

        #region GetLastRecord
        /// <summary>
        /// 获取指定客户ID的最新一条客户跟进记录
        /// </summary>
        /// <param name="ccCustomerInfoId"></param>
        /// <returns></returns>
        public CustomerFollowUpRecordDto GetLastRecord(long ccCustomerInfoId)
        {
            var a = customerFollowUpRecordData.GetLastRecord(ccCustomerInfoId).GetAwaiter().GetResult();
            return a.As<CustomerFollowUpRecordDto>();
        }
        #endregion

        #region GetListByCustomer ByPage
        public PageableList<CustomerFollowUpRecordDto> GetListByCustomer(long ccCustomerInfoId, int ccPageNo, int ccPageSize)
        {
            var (fCount,fList) = customerFollowUpRecordData.GetListByCustomerAsync(ccCustomerInfoId, ccPageNo, ccPageSize).GetAwaiter().GetResult();

            return new PageableList<CustomerFollowUpRecordDto>()
            {
                Count = fCount,
                Items = fList.As<List<CustomerFollowUpRecordDto>>(),
                PageIndex = ccPageNo,
                PageSize = ccPageSize
            };
        }
        #endregion
    }
}